package library.jaas;

import java.io.Serializable;
import java.security.Principal;
import java.security.acl.Group;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * The Class MyGroup.
 * 
 * @author Matthijs Steen
 */
public class MyGroup implements Group, Serializable
{
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The name. */
	private final String name;
	
	/** The members. */
	private final Set<Principal> members = new HashSet<Principal>();
	
	/**
	 * Instantiates a new my group.
	 * 
	 * @param name the name
	 */
	public MyGroup(String name)
	{
		this.name = name;
	}
	
	/* (non-Javadoc)
	 * @see java.security.acl.Group#addMember(java.security.Principal)
	 */
	@Override
	public boolean addMember(Principal member)
	{
		return members.add(member);
	}
	
	/* (non-Javadoc)
	 * @see java.security.acl.Group#isMember(java.security.Principal)
	 */
	@Override
	public boolean isMember(Principal member)
	{
		return members.contains(member);
	}
	
	/* (non-Javadoc)
	 * @see java.security.acl.Group#members()
	 */
	@Override
	public Enumeration<? extends Principal> members()
	{
		return Collections.enumeration(members);
	}
	
	/* (non-Javadoc)
	 * @see java.security.acl.Group#removeMember(java.security.Principal)
	 */
	@Override
	public boolean removeMember(Principal member)
	{
		return members.remove(member);
	}
	
	/* (non-Javadoc)
	 * @see java.security.Principal#getName()
	 */
	@Override
	public String getName()
	{
		return name;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o)
	{
		return (o instanceof MyGroup ? ((MyGroup) o).getName().equals(name) : false);
	}
}
